﻿namespace PayPal.ButtonManager
{
    using System;
    using System.Collections.Generic;

    using com.paypal.sdk.util;

    public static class NameValuePairApiHelper
    {
        public static void PrepareButtonVars(NVPCodec encoder, Dictionary<string, string> vars, ButtonOption[] opts)
        {
            if (vars != null)
            {
                int i = 0;
                foreach (KeyValuePair<string, string> kvp in vars)
                {
                    string key = kvp.Key;
                    string value = kvp.Value;
                    string data = key + "=" + value;
                    encoder.Add("L_BUTTONVAR" + i, data);
                    i++;
                }
            }

            if (opts != null)
            {
                int j = 0;

                foreach (ButtonOption op in opts)
                {
                    encoder.Add("OPTION" + j + "NAME", op.Name);

                    int k = 0;

                    foreach (ButtonOptionItem item in op.OptionItems)
                    {
                        encoder.Add("L_OPTION" + j + "SELECT" + k, item.SelectName);
                        if (!String.IsNullOrEmpty(item.Price))
                        {
                            encoder.Add("L_OPTION" + j + "PRICE" + k, item.Price);
                        }

                        if (!String.IsNullOrEmpty(item.BillingPeriod))
                        {
                            encoder.Add("L_OPTION" + j + "BILLINGPERIOD" + k, item.BillingPeriod);
                        }

                        if (item.BillingFrequency != 0)
                        {
                            encoder.Add("L_OPTION" + j + "BILLINGFREQUENCY" + k, item.BillingFrequency.ToString());
                        }

                        if (item.TotalBillingCycles != 0)
                        {
                            encoder.Add("L_OPTION" + j + "TOTALBILLINGCYCLES" + k, item.TotalBillingCycles.ToString());
                        }

                        if (!String.IsNullOrEmpty(item.Amount))
                        {
                            encoder.Add("L_OPTION" + j + "AMOUNT" + k, item.Amount);
                        }

                        if (!String.IsNullOrEmpty(item.ShippingAmount))
                        {
                            encoder.Add("L_OPTION" + j + "SHIPPINGAMOUNT" + k, item.ShippingAmount);
                        }

                        if (!String.IsNullOrEmpty(item.TaxAmount))
                        {
                            encoder.Add("L_OPTION" + j + "TAXAMOUNT" + k, item.TaxAmount);
                        }

                        k++;
                    }

                    j++;
                }
            }
        }

        public static ButtonManagerResponse PrepareResponse(NVPCodec decoder)
        {
            string webSiteCode = decoder.Get("WEBSITECODE");
            string emailLink = decoder.Get("EMAILLINK");
            string hostedButtonId = decoder.Get("HOSTEDBUTTONID");

            ButtonManagerResponse response = new ButtonManagerResponse();
            response.EmailLink = emailLink;
            response.HostedButtonId = hostedButtonId;
            response.WebSiteCode = webSiteCode;

            return response;
        }

        public static NVPCodec PrepareEncoder(
                            string method,
                            string buttonType = "",
                            string buttonCode = "",
                            string buttonStatus = "",
                            string hostedButtonId = "",
                            string buttonImage = "",
                            string buttonImageUrl = "",
                            string buttonSubType = "",
                            string buttonCountry = "",
                            string buttonLanguage = "",
                            string buyNowText = "",
                            string subscribeText = "")
        {
            NVPCodec encoder = new NVPCodec();

            encoder.Add("VERSION", "51.0");
            encoder.Add("METHOD", method);

            if (!String.IsNullOrEmpty(buttonCode))
            {
                encoder.Add("BUTTONCODE", buttonCode);
            }

            if (!String.IsNullOrEmpty(buttonType))
            {
                encoder.Add("BUTTONTYPE", buttonType);
            }

            if (!String.IsNullOrEmpty(buttonSubType))
            {
                encoder.Add("BUTTONSUBTYPE", buttonSubType);
            }

            if (!String.IsNullOrEmpty(buttonStatus))
            {
                encoder.Add("BUTTONSTATUS", buttonStatus);
            }

            if (!String.IsNullOrEmpty(hostedButtonId))
            {
                encoder.Add("HOSTEDBUTTONID", hostedButtonId);
            }

            if (!String.IsNullOrEmpty(buttonImage))
            {
                encoder.Add("BUTTONIMAGE", buttonImage);
            }

            if (!String.IsNullOrEmpty(buttonImageUrl))
            {
                encoder.Add("BUTTONIMAGEURL", buttonImageUrl);
            }

            if (!String.IsNullOrEmpty(buttonCountry))
            {
                encoder.Add("BUTTONCOUNTRY", buttonCountry);
            }

            if (!String.IsNullOrEmpty(buttonLanguage))
            {
                encoder.Add("BUTTONLANGUAGE", buttonLanguage);
            }

            if (!String.IsNullOrEmpty(buyNowText))
            {
                encoder.Add("BUYNOWTEXT", buyNowText);
            }

            if (!String.IsNullOrEmpty(subscribeText)) 
            { 
                encoder.Add("SUBSCRIBETEXT", subscribeText); 
            }

            return encoder;
        }
    }
}
